package testes.vSys.action;

import java.util.Collection;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import testes.vSys.bean.EventType;
import testes.vSys.bean.Moviment;
import testes.vSys.bean.Vehicle;
import testes.vSys.dao.DAOFactory;
import testes.vSys.dao.IMovimentDao;
import testes.vSys.dao.IVehicleDao;
import testes.vSys.form.MovimentForm;

public class MovimentAction extends DispatchAction{

	private final String VEHICLES_COLLECTION = "vehiclesCollection";
	private final String VEHICLE = "vehicle";
	private final String EVENT_COLLECTION = "eventsCollection";
	private final String MOVIMENTS_COLLECTION = "movimentsCollection";
	
	private final String MSG_CONFIRMATION = "msgConfirmation";
	
	public ActionForward load(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
		IVehicleDao vehicleDAO = DAOFactory.getVehicleDao();
	    IMovimentDao movimentDAO = DAOFactory.getMovimentDao();
	    
		
		Collection<Vehicle> vehicles = vehicleDAO.getVehicles();
		Collection<EventType> eventTypes = movimentDAO.getAvaiableEvents();
		
		request.setAttribute(VEHICLES_COLLECTION, vehicles);
		request.setAttribute(EVENT_COLLECTION, eventTypes);
		
		return mapping.findForward("moviment");
	}
	
	
	public ActionForward view(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		IVehicleDao vehicleDAO = DAOFactory.getVehicleDao();
		IMovimentDao movimentDao = DAOFactory.getMovimentDao();
		
		Collection<Moviment> moviments = movimentDao.getMovimentByVehicle(vehicleDAO.getVehicleByPlate(((MovimentForm)form).getPlate()));
		
		Vehicle vehicle = vehicleDAO.getVehicleByPlate(((MovimentForm)form).getPlate());
		
		request.setAttribute(VEHICLE,vehicle);
		request.setAttribute(MOVIMENTS_COLLECTION, moviments);
		
		return mapping.findForward("view");
	}
	
	public ActionForward insert(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {

		System.out.println("insert");
		
		IMovimentDao movimentDao = DAOFactory.getMovimentDao();

		MovimentForm movimentForm = (MovimentForm)form;

		Moviment moviment = new Moviment();
		
		moviment.setDate(new Date());
		moviment.setVehicle(DAOFactory.getVehicleDao().getVehicleByPlate(movimentForm.getPlate()));
		moviment.setKm(Integer.parseInt(movimentForm.getKm()));
		moviment.setValue(Integer.parseInt(movimentForm.getValue()));
		moviment.setEventType(movimentDao.getEvent(Integer.parseInt(movimentForm.getEventType())));
	
		request.setAttribute(MSG_CONFIRMATION, "Movimentação '" + movimentDao.getEvent(Integer.parseInt(movimentForm.getEventType())).getEventType()  + "' para o veículo '"+ movimentForm.getPlate() +"' incluída com sucesso.");
		
		movimentDao.createMoviment(moviment);
		movimentForm.resetForm();
		
		
		return load(mapping, form, request, response);
	}
	
	public ActionForward clearFields(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		MovimentForm movimentForm = (MovimentForm)form;
		movimentForm.resetForm();
		
		return load(mapping, form, request, response);
	}

}
